/*
 * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang.ref;


/**
 * Phantom reference objects, which are enqueued after the collector
 * determines that their referents may otherwise be reclaimed.  Phantom
 * references are most often used for scheduling pre-mortem cleanup actions in
 * a more flexible way than is possible with the Java finalization mechanism.
 *
 * <p> If the garbage collector determines at a certain point in time that the
 * referent of a phantom reference is <a
 * href="package-summary.html#reachability">phantom reachable</a>, then at that
 * time or at some later time it will enqueue the reference.
 *
 * <p> In order to ensure that a reclaimable object remains so, the referent of
 * a phantom reference may not be retrieved: The <code>get</code> method of a
 * phantom reference always returns <code>null</code>.
 *
 * <p> Unlike soft and weak references, phantom references are not
 * automatically cleared by the garbage collector as they are enqueued.  An
 * object that is reachable via phantom references will remain so until all
 * such references are cleared or themselves become unreachable.
 * <p>
 * 1. 虚引用(PhantomReference)与引用队列(ReferenceQueue)的结合使用流程，结合代码进行说明：
 * <pre>{@code
 * import java.lang.ref.PhantomReference;
 * import java.lang.ref.ReferenceQueue;
 *
 * public class PhantomReferenceDemo {
 *
 *     static class Resource {
 *         private byte[] data = new byte[1024 * 1024]; // 模拟占用1MB内存
 *
 *         // 资源清理方法（如释放文件句柄、堆外内存等）
 *         protected void finalize() throws Throwable {
 *             System.out.println("Resource 被垃圾回收");
 *         }
 *     }
 *
 *     public static void main(String[] args) throws InterruptedException {
 *         // 1. 创建引用队列
 *         ReferenceQueue<Resource> referenceQueue = new ReferenceQueue<>();
 *
 *         // 2. 创建【虚引用】并【关联目标对象】和【引用队列】
 *         Resource resource = new Resource();// 创建目标对象
 *         PhantomReference<Resource> phantomRef = new PhantomReference<>(resource, referenceQueue);
 *
 *         // 3. 目标对象被回收时, 虚引用自动入队
 *         // 3.1 释放强引用，使对象可被回收（对象失去强引用后，该对象就变为不可达，GC 就会将其标记为可回收。）
 *         resource = null;
 *         // 3.2 强制触发GC（仅用于演示，生产环境慎用）
 *         System.gc();
 *         // 3.3 等待GC完成（虚引用入队需要时间）
 *         Thread.sleep(500);
 *
 *         // 4. 检查队列中的引用
 *         // poll() 方法非阻塞，立即返回队首引用（若无则返回 null）。可调用 remove() 方法阻塞等待，直到有引用入队。
 *         java.lang.ref.Reference<? extends Resource> ref = referenceQueue.poll();
 *         if (ref != null) {
 *             System.out.println("检测到虚引用入队，对象已被回收");
 *             // 执行资源清理操作（如释放堆外内存）
 *             // ref.clear(); // 可选：显式断开引用
 *         }
 *     }
 * }
 * </pre>
 *
 * @author   Mark Reinhold
 * @since    1.2
 */

public class PhantomReference<T> extends Reference<T> {

    /**
     * Returns this reference object's referent.  Because the referent of a
     * phantom reference is always inaccessible, this method always returns
     * <code>null</code>.
     *
     * @return  <code>null</code>
     */
    public T get() {
        return null;
    }

    /**
     * Creates a new phantom reference that refers to the given object and
     * is registered with the given queue.
     * 创建一个新的虚引用 用来引用 给定的对象。
     *
     * <p> It is possible to create a phantom reference with a <tt>null</tt>
     * queue, but such a reference is completely useless: Its <tt>get</tt>
     * method will always return null and, since it does not have a queue, it
     * will never be enqueued.
     *
     * @param referent the object the new phantom reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     */
    public PhantomReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }

}
